<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="160px">
<!--      <el-form-item prop="warehouseId">-->
<!--        <el-input v-model="dataForm.warehouseId"></el-input>-->
<!--      </el-form-item>-->
      <el-form-item prop="zoneCode" label="库区编码">
        <el-input v-model="dataForm.zoneCode" placeholder="请输入库区编码"></el-input>
      </el-form-item>
      <el-form-item prop="archiveId" label="全宗编号">
        <el-select placeholder="请选择所属全宗" @change="handle" v-model="dataForm.archiveId">
          <el-option v-for="generalArchive in generalArchiveList" :key="generalArchive.id" :label="generalArchive.name" :value="generalArchive.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="zoneName" label="库区名称">
        <el-input v-model="dataForm.zoneName" placeholder="请输入库区名称"></el-input>
      </el-form-item>
      <el-form-item prop="area" label="密集架范围">
        <el-button type="primary" @click="innerVisible = true">打开密集架</el-button>
      </el-form-item>
      <el-form-item prop="dz" label="库区编码设置">

      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>

  <el-dialog title="密集架" v-model="innerVisible" :size="800" close-on-press-escape="false" custom-class="rr-drawer">
    <reservoir-area-Location :propsData="propsData"  @getLocationList="getLocationList"></reservoir-area-Location>
  </el-dialog>
</template>

<script lang="ts">
import { defineComponent, reactive, toRefs } from "vue";
import baseService from "@/service/baseService";
import useView from "@/hooks/useView";
import { useDebounce } from "@/utils/utils";
import reservoirAreaLocation from "./reservoir-area-location.vue";
import { IObject } from "@/types/interface";
export default defineComponent({
  components: {
    reservoirAreaLocation
  },props:{
    warehouseIdArea:String
  },
  setup(props) {
    const state = reactive({
      getDataListURL: "/archive/class/list",
      deleteURL: "/archive/class/",
      generalArchiveList: [],
      visible: false,
      innerVisible:false,
      propsData:{
        warehouseId: props.warehouseIdArea,
        locationIds:[] as IObject,
      },
      dataForm: {
        id: "",
        archiveId: "",
        zoneCode: "",
        zoneName: "",
        warehouseId:props.warehouseIdArea,
        archiveWarehouseLocationIds:[] as any[],
        archiveWarehouseLocationList: [] as any[]
      }
    });
    return { ...useView(state), ...toRefs(state) };
  },
  watch: {
  },
  computed: {
    dataRule() {
      return {
        zoneCode: [{ required: true, message: "库区编码不能为空", trigger: "blur" }],
        zoneName: [{ required: true, message: "库区名字不能为空", trigger: "change" }],
      };
    }
  },
  created() {
    baseService.get(`/archive/info/list`).then((res) => {
      if (res.code !== 0) {
        return this.$message.error(res.msg);
      }
      this.generalArchiveList = res.data;
    });
    this.dataFormSubmitHandle = useDebounce(this.dataFormSubmitHandle);
  },
  methods: {
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataFormRef"].resetFields();
        this.getInfo();
      });
    },

    getInfo() {
      if (this.dataForm.id != "") {
        baseService.get(`/archive/warehouse-zone/${this.dataForm.id}`).then((res) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dataForm = {
            ...this.dataForm,
            ...res.data
          };
          this.propsData.locationIds = res.data.archiveWarehouseLocationList
        });
      }
    },
    dataFormSubmitHandle() {
      this.$refs["dataFormRef"].validate((valid: boolean) => {
        if (!valid && !this.dataForm.id) {
          return false;
        }
        (!this.dataForm.id ? baseService.post : baseService.put)("/archive/warehouse-zone", {
          ...this.dataForm
        }).then((res) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.$message({
            message: this.$t("prompt.success"),
            type: "success",
            duration: 500,
            onClose: () => {
              this.visible = false;
              this.$emit("refreshDataList");
            }
          });
        });
      });
    },
    handle(v) {
      baseService.get(`/archive/class/list`, { archiveId: v }).then((res) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg);
        }
        this.dataList = res.data;
        console.log(res);
      });
    },
    getLocationList(data){
      console.log(data)
      this.dataForm.archiveWarehouseLocationIds = data
    }
  }
});
</script>

<style lang="less">
.mod-tenant__tenant {
  .role-list {
    .el-select {
      width: 100%;
    }
  }
}

.el-dialog__body {
  width: 100%;
}

.plus_one {
  position: absolute;
  top: 1px;
  right: -45px;
}

// }
</style>
